package com.yami.shop.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.vo.UserCashOutVo;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.service.OrderItemService;
import com.yami.shop.service.OrderRefundService;
import com.yami.shop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/homePage")
public class HomePageController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderRefundService orderRefundService;

    /**
     * 首页上方统计信息
     * @return
     */
    @GetMapping("/statistics")
    public ServerResponseEntity<Map<String,Object>> statistics() {
        Date date = new Date();
        DateTime dateTime = DateUtil.beginOfMonth(date);
        DateTime dateTime1 = DateUtil.endOfMonth(date);

        Map<String,Object> rmap=new HashMap<>();

        //本月所有订单
        List<Order> list = orderService.lambdaQuery()
                .select(Order::getOrderNumber)
                .between(Order::getPayTime, dateTime, dateTime1)
                .eq(Order::getIsPayed, 1)//已支付
                .in(Order::getStatus, Arrays.asList(2, 3, 4, 5)).list();

        List<String> collect = list.stream().map(o -> o.getOrderNumber()).collect(Collectors.toList());
        if(collect.isEmpty()){
            return ServerResponseEntity.showFailMsg("本月无订单");
        }
        //查询订单子表
        List<OrderItem> list1 = orderItemService.lambdaQuery()
                .select(OrderItem::getProdCount,OrderItem::getProductTotalAmount)
                .in(OrderItem::getOrderNumber, collect).list();
        //本月总销售商品数量
        int sum = list1.stream().mapToInt(i -> i.getProdCount()).sum();
        //本月销售总金额
        double sum1 = list1.stream().mapToDouble(i -> i.getProductTotalAmount()).sum();

        rmap.put("monthSaleNumber",sum);
        rmap.put("monthSaleMoney",sum1);

        //查询退款信息
        List<OrderRefund> list2 = orderRefundService.lambdaQuery()
                .select(OrderRefund::getGoodsNum,OrderRefund::getRefundAmount)
                .in(OrderRefund::getOrderNumber, collect)
                .eq(OrderRefund::getReturnMoneySts, 1).list();

        //退款数量
        int sumRefund = list2.stream().mapToInt(i -> i.getGoodsNum()).sum();
        BigDecimal reduce = list2.stream().map(r -> r.getRefundAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);

        rmap.put("monthRefundNumber",sumRefund);
        rmap.put("monthRefundMoney",reduce);

        return ServerResponseEntity.success(rmap);
    }

    /*
    DateTime dateTime = DateUtil.beginOfMonth(date);
    for(int a=0;a<4;a++){
            //找到每个星期内的一个时间
            DateTime dateTime2 = DateUtil.offsetDay(dateTime, a * 7);
            //从凌晨向后偏移5分钟
            DateTime offset = DateUtil.offset(dateTime2, DateField.MINUTE, 5);

            DateTime dateTime3 = DateUtil.beginOfWeek(offset);
            DateTime dateTime4 = DateUtil.endOfWeek(offset);
            allWeek.add(Arrays.asList(dateTime3,dateTime4));
        }*/

    /**
     * 月统计
     * @return
     */
    @GetMapping("/weekStatistics")
    public ServerResponseEntity<List> weekStatistics() {
        Date date = new Date();
        List<List<Date>> allWeek=new ArrayList<>();

        //往前减4个月
        DateTime offset1 = DateUtil.offset(date, DateField.MONTH, -3);

        for(int a=0;a<4;a++) {
            DateTime offset = DateUtil.offset(offset1, DateField.MONTH, a);

            DateTime dateTime3 = DateUtil.beginOfMonth(offset);
            DateTime dateTime4 = DateUtil.endOfMonth(offset);

            allWeek.add(Arrays.asList(dateTime3,dateTime4));
        }

        List<Map<String,Object>> list3=new ArrayList<>();

        for(List<Date> dateRange:allWeek){
            String format = DateUtil.format(dateRange.get(0), "yyyy-MM");
            Map<String,Object> rmap=new HashMap<>();
            rmap.put("month",format);


            List<Order> list = orderService.lambdaQuery()
                    .select(Order::getOrderNumber)
                    .between(Order::getPayTime, dateRange.get(0), dateRange.get(1))
                    .eq(Order::getIsPayed, 1)//已支付
                    .in(Order::getStatus, Arrays.asList(2, 3, 4, 5)).list();

            List<String> collect = list.stream().map(o -> o.getOrderNumber()).collect(Collectors.toList());
            if(collect.isEmpty()){
                rmap.put("monthSaleNumber",0);
                rmap.put("monthSaleMoney",0);
                rmap.put("monthRefundNumber",0);
                rmap.put("monthRefundMoney",0);
                list3.add(rmap);
                continue;
            }

            //查询订单子表
            List<OrderItem> list1 = orderItemService.lambdaQuery()
                    .select(OrderItem::getProdCount,OrderItem::getProductTotalAmount)
                    .in(OrderItem::getOrderNumber, collect).list();


            //本月总销售商品数量
            int sum = list1.stream().mapToInt(i -> i.getProdCount()).sum();
            //本月销售总金额
            double sum1 = list1.stream().mapToDouble(i -> i.getProductTotalAmount()).sum();

            rmap.put("monthSaleNumber",sum);
            rmap.put("monthSaleMoney",sum1);

            //查询退款信息
            List<OrderRefund> list2 = orderRefundService.lambdaQuery()
                    .select(OrderRefund::getGoodsNum,OrderRefund::getRefundAmount)
                    .in(OrderRefund::getOrderNumber, collect)
                    .eq(OrderRefund::getReturnMoneySts, 1).list();

            //退款数量
            int sumRefund = list2.stream().mapToInt(i -> i.getGoodsNum()).sum();
            BigDecimal reduce = list2.stream().map(r -> r.getRefundAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);

            rmap.put("monthRefundNumber",sumRefund);
            rmap.put("monthRefundMoney",reduce);

            list3.add(rmap);
        }
        return ServerResponseEntity.success(list3);
    }

    /**
     * 获取待发货订单
     * @return
     */
    @GetMapping("/getPendingShipmentOrder")
    public ServerResponseEntity<List<Order>> getPendingShipmentOrder() {
        List<Order> list = orderService.lambdaQuery()
                .eq(Order::getIsPayed, 1)//已支付
                .eq(Order::getStatus, 2)//代发货
                .orderByDesc(Order::getPayTime)
                .list();

        return ServerResponseEntity.success(list);
    }
}
